package 其他;

//import lombok.*;
import java.util.*;

//@Data
//@AllArgsConstructor
//@NoArgsConstructor
class BTreeNode{

     int val;
     BTreeNode left;
     BTreeNode right;

     BTreeNode(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

//@Data
//@NoArgsConstructor
//@AllArgsConstructor
class BTree{
    public BTreeNode root;

    //创建连续值的二叉树  尚未完成
    public void creatTree(BTreeNode node,int data){
        if(root == null) {
            root = new BTreeNode(data);
        }else{
            if(node.left == null){
                node.left = new BTreeNode(data);
            }else{
                creatTree(node.left, data);
            } if(root.right == null){
                node.right = new BTreeNode(data);
            }else {
                creatTree(node.right, data);
            }
        }
    }
}

public class test07 {


    public  int solve(BTreeNode root, int a, int b){
        int count1 = 0;
        int count2 = 0;
        if(root == null)
            return -1;
        if(root.val == a || root.val == b)
            return root.val;

        int left = solve(root.left, a, b);
        count1++;
        int right = solve(root.right, a, b);
        count2++;

        if(left == -1)
            return right;
        if(right == -1)
            return left;
        return count1 + count2;
    }

    public static void main(String[] args) {
        BTree tree = new BTree();
        test07 t = new test07();
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int[][] arr = new int[num][2];
        int max = 0;
        for (int i = 0; i < num; i++) {
            arr[i][0] = sc.nextInt();
            arr[i][1] = sc.nextInt();
            if(arr[i][1] > max)
                max = arr[i][1];
        }
        for (int i = 1; i <= max; i++) {
            tree.creatTree(tree.root,i);
        }


        for (int i = 0; i < num; i++) {
            int result = t.solve(tree.root, arr[i][0], arr[i][1]);
            System.out.println(result);
        }
    }
}
